package com.duobaoyu.dby.supply.biz.enums;

import com.duobaoyu.dby.common.core.constant.StringPool;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName RedisKeys
 * @Description 缓存Key枚举
 * @createTime 14:29:00 2022/05/10
 */
@Slf4j
@AllArgsConstructor
public enum RedisKeys {

	/**
	 * 分布式锁KEY
	 */
	LOCK_REDIS("SUPPLY:LOCK:{1}", "分布式锁KEY"),
	/**
	 * 发票信息用于订单下单处理
	 */
	INVOICE("SUPPLY:INVOICE:{1}", "发票信息"),
	/**
	 * SUPPLY:CHANNEL:PATH:{hostname(域名)}
	 */
	CHANNEL_PATH_KEY("SUPPLY:CHANNEL:PATH:{1}", "对接渠道回调地址映射"),
	/**
	 * 渠道余额缓存Key
	 */
	CHANNEL_BALANCE_KEY("SUPPLY:CHANNEL:BALANCE:KEY","渠道余额缓存Key"),;

	private static final PropertyPlaceholderHelper PLACE_HOLDER_HELPER = new PropertyPlaceholderHelper(StringPool.DELIM_START, StringPool.DELIM_END);
	private String cacheKeyPrefix;
	private String desc;

	public String key() {
		return this.cacheKeyPrefix;
	}

	public String desc() {
		return this.desc;
	}

	public String formatKey(final Object... keys) {
		final String template = this.cacheKeyPrefix;
		return PLACE_HOLDER_HELPER.replacePlaceholders(template, (placeholderName) -> {
			if (Objects.equals("1",placeholderName)) {
				if (ArrayUtils.isEmpty(keys)) {
					log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
				}
				return keys[0].toString();
			}
			if (Objects.equals("2",placeholderName)) {
				if (keys.length < 2) {
					log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
				}
				return keys[1].toString();
			}
			if (Objects.equals("3",placeholderName)) {
				if (keys.length < 3) {
					log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
				}
				return keys[2].toString();
			}
			if (Objects.equals("4",placeholderName)) {
				if (keys.length < 4) {
					log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
				}
				return keys[3].toString();
			}
			return StringPool.EMPTY;
		});
	}
}
